package com.yongcheon.sms_manager.activities;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.mocoplex.adlib.AdlibActivity;
import com.mocoplex.adlib.AdlibConfig;
import com.mocoplex.adlib.AdlibPop;
import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.contract.ContactData;
import com.yongcheon.sms_manager.data.phone_spsm.PhoneData;
import com.yongcheon.sms_manager.data.phone_spsm.SpamPhonData;
import com.yongcheon.sms_manager.data.sms_spam.SMSData;
import com.yongcheon.sms_manager.data.sms_spam.SpamData;
import com.yongcheon.sms_manager.ui.phone_spam.actionTab.ActionTabPhoneService;
import com.yongcheon.sms_manager.ui.sms_spam.actionTab.ActionTabSMSService;
import com.yongcheon.sms_manager.utils.AndroidSystemUtil;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;


public class MainActivity extends AdlibActivity {
    public static final int DEFAULT_SETTING_FLAG_SET = 1;
    public static final int DEFAULT_SETTING_FLAG_UNSET = 2;
    public static final int STATUS_SETTING = 3;
    public static final int STATUS_NORMAL = 1;
    public static final String EXTRA_SPAM_ADDR = "ext_spam_addr";
    public static final String EXTRA_SPAM_DATE = "ext_spam_date";
    public static final String EXTRA_SPAM_ID = "ext_spam_id";
    public static final String EXTRA_SPAM_CATEGORY = "ext_spam_category";
    public static final String EXTRA_SPAM_SUBJECT = "ext_spam_subject";

    public static int status = 0;
    public static boolean switch_status = true;

    Bundle mainBundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setActionTab(savedInstanceState);

        // adlib 관련 설정
        initAds();
        this.setAdsContainer(R.id.ads);
        setAdlibPopFrameColor(Color.DKGRAY);  // default 는 0xff000000
        setAdlibPopCloseButtonStyle(AdlibPop.BTN_WHITE);  // default는 BTN_WHITE
        setAdlibPopAnimationType(AdlibPop.ANIMATION_SLIDE, AdlibPop.ANIMATION_SLIDE); // default는 ANIMATION_SLIDE
        showAdlibPop(AdlibPop.ALIGN_BOTTOM, 30);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        Bundle bundle = getIntent().getExtras();
//        if(bundle != null) {
//            String addr = bundle.getString(MainActivity.EXTRA_SPAM_ADDR);
//            System.out.println("addr : " + addr);
//        }
//        setDefaultApp(DEFAULT_SETTING_FLAG_UNSET);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setDefaultApp(DEFAULT_SETTING_FLAG_UNSET);
    }

    @Override
    protected void onResume() {
        super.onResume();
        status = STATUS_NORMAL;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
//        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        MenuItem network = menu.findItem(R.id.switch_item);
        Switch sw = (Switch)network.getActionView();
        sw.setChecked(switch_status);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked == false) {
                    switch_status = false;
                }else{
                    switch_status = true;
                }
                getActionBar().removeAllTabs();
                setActionTab(mainBundle);
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean returnBool = true;
        switch (item.getItemId()) {
            case R.id.preference_settings :
                status = STATUS_SETTING;
                Intent intent = new Intent(this, PreferenceSettingActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                startActivity(intent);
            break;
        }
        return returnBool;
    }

    public void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
        outState.putInt("seltab", getActionBar().getSelectedNavigationIndex());
    }

    public void setActionTab(Bundle savedInstanceState){
        Bundle mainBundle = savedInstanceState;
        ActionBar ab = getActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        if(switch_status == true) {
            ActionTabSMSService actionTabService = new ActionTabSMSService();
            actionTabService.setContactData(new ContactData(this.getContentResolver()));
            actionTabService.setSMSService(new SMSData());
            actionTabService.createActionTab(ab, getResources().getString(R.string.total));
            actionTabService.setSMSService(new SpamData(this.getApplicationContext()));
            actionTabService.createActionTab(ab, getResources().getString(R.string.spam_sms));
        }else{
            ActionTabPhoneService phoneService = new ActionTabPhoneService();
            phoneService.setContactData(new ContactData(this.getContentResolver()));
            phoneService.setPhoneService(new PhoneData());
            phoneService.createActionTab(ab, getResources().getString(R.string.receive_phone));
            phoneService.setPhoneService(new SpamPhonData());
            phoneService.createActionTab(ab, getResources().getString(R.string.spam_phone));
        }

        if (savedInstanceState != null) {
            int seltab = savedInstanceState.getInt("seltab");
            ab.setSelectedNavigationItem(seltab);
        }
    }

    public void setDefaultApp(int flag){
        if(flag == DEFAULT_SETTING_FLAG_SET) {
            if (!DefaultAppUtil.checkIsDefault(this)) {
                DefaultAppUtil.setDefaultAppSHow(this, DefaultAppUtil.REQ_FLAG_NO_RESULT, -1, null);
            }
        }else if(flag == DEFAULT_SETTING_FLAG_UNSET){
            if(DefaultAppUtil.checkIsDefault(this) && (AndroidSystemUtil.ANDROID_VERSION > 16))  {
                if(status != STATUS_SETTING) DefaultAppUtil.restoreDefaultApp(this);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("======================= requestCode"+requestCode);
        System.out.println("======================= resultCode"+resultCode);
        System.out.println("======================= data"+data);
    }

    protected void initAds(){
        AdlibConfig.getInstance().bindPlatform("ADAM","adlib.project.ads.SubAdlibAdViewAdam");
        AdlibConfig.getInstance().bindPlatform("ADMOB","adlib.project.ads.SubAdlibAdViewAdmob");
        AdlibConfig.getInstance().setAdlibKey("54a355f30cf2c668c799954a");
    }
}
